package com.bops.web.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.bops.biz.permission.GrantManager;
import com.bops.dal.information.dataobject.NewsInfoDO;
import com.bops.dal.information.queryobject.NewsInfoQuery;
import com.bops.service.CommonServiceLocator;
import com.bops.service.NewsInfoService;
import com.bops.valve.webuser.BopsWebUser;
import com.yue.commons.seine.web.annotations.ControllerAction;
import com.yue.commons.seine.web.servlet.mvc.AbstractController;
import com.yue.commons.seine.web.servlet.result.View;
import com.yue.commons.seine.web.servlet.result.WebResult;

public class InfosController extends AbstractController {

	private NewsInfoService newsService = CommonServiceLocator.getNewsService();

	/**
	 * 修改
	 * */
	@ControllerAction
	public WebResult viewNews(Map<String, Object> model,Integer id,String action){
		if("edit".equals(action)){
			NewsInfoDO newsDO = newsService.getById(id);
			 if (newsDO == null) {
	                model.put("errorStatus", "没有编号是" + id + "的记录!");
	                return null;
	         }
			 model.put("newsDO", newsDO);
		}
		return new View("/info/editNews.htm");
	}
	
	/**
	 * 编辑新闻
	 * */
	@ControllerAction
	public WebResult editNewsForm(Map<String, Object> model,NewsInfoDO newsDO,String action){
		if ("add".equals(action)) {
			Integer id = newsService.insert(newsDO);
			model.put("actionStatus", "添加成功!请继续添加!");
			
        } else {
        	newsService.update(newsDO);
            model.put("actionStatus", "修改成功!");
        }
		return  serachNews(model);
		
	}
	
	
	
	/**
	 * 添加新闻
	 * */
	@ControllerAction
	public WebResult editNews(Map<String, Object> model,NewsInfoDO newsDO) {
		BopsWebUser bopsWebUser = BopsWebUser.getCurrentUser();
		newsDO.setPublisher(bopsWebUser.getName());
		model.put("newsDO", newsDO);
		return new View("/info/editNews.htm");

	}

	@ControllerAction
	public WebResult serachNews(Map<String, Object> model){
		return new View("/info/searchNews.htm");
		
	}
	/**
	 * 获取全部新闻
	 * */
	@ControllerAction
	public WebResult serachNewsForm(Map<String, Object> model,
			NewsInfoQuery newsQuery) {
		List<NewsInfoDO> newsList = newsService.listNews(newsQuery);
		if (newsList.size() > 0 || newsList != null) {
			model.put("newsList", newsList);
			model.put("newsDO", newsQuery);
			model.put("pagniation", newsList);
			if (GrantManager.hasPermission("news_add_role")) {
				model.put("news_add_role", "true");
			}
		}
		return new View("/info/searchNews.htm");

	}
}
